import Vue from 'vue'
import Router from 'vue-router'
import {mapGetters} from 'vuex'
import store from '@/store/index'

Vue.use(Router)

const Layout = (resolve) => {
  import('@/views/Layout').then((module) => {
    resolve(module)
  })
}

const IndexView = (resolve) => {
  import('@/views/IndexView').then((module) => {
    resolve(module)
  })
}

const GuideView = (resolve) => {
  import('@/views/GuideView').then((module) => {
    resolve(module)
  })
}

const MineView = (resolve) => {
  import('@/views/MineView').then((module) => {
    resolve(module)
  })
}

const SearchView = (resolve) => {
  import('@/views/SearchView').then((module) => {
    resolve(module)
  })
}

const ProductDetailView = (resolve) => {
  import('@/views/ProductDetailView').then((module) => {
    resolve(module)
  })
}

const LoginView = (resolve) => {
  import('@/views/LoginView').then((module) => {
    resolve(module)
  })
}

const SponsorView = (resolve) => {
  import('@/views/SponsorView').then((module) => {
    resolve(module)
  })
}

const NewUserView = (resolve) => {
  import('@/views/NewUserView').then((module) => {
    resolve(module)
  })
}

const DataReportView = (resolve) => {
  import('@/views/DataReportView').then((module) => {
    resolve(module)
  })
}

const UserSetView = (resolve) => {
  import('@/views/UserSetView').then((module) => {
    resolve(module)
  })
}

const UserAuthView = (resolve) => {
  import('@/views/UserAuthView').then((module) => {
    resolve(module)
  })
}

const SucCaseView = (resolve) => {
  import('@/views/SucCaseView').then((module) => {
    resolve(module)
  })
}

const MessageView = (resolve) => {
  import('@/views/MessageView').then((module) => {
    resolve(module)
  })
}

const MessageDetailView = (resolve) => {
  import('@/views/MessageDetailView').then((module) => {
    resolve(module)
  })
}

const MyOrderView = (resolve) => {
  import('@/views/MyOrderView').then((module) => {
    resolve(module)
  })
}

const MyAddressView = (resolve) => {
  import('@/views/MyAddressView').then((module) => {
    resolve(module)
  })
}

const MyCalendarView = (resolve) => {
  import('@/views/MyCalendarView').then((module) => {
    resolve(module)
  })
}

const MyProjectView = (resolve) => {
  import('@/views/MyProjectView').then((module) => {
    resolve(module)
  })
}

const LinkUsView = (resolve) => {
  import('@/views/LinkUsView').then((module) => {
    resolve(module)
  })
}

const RiskView = (resolve) => {
  import('@/views/RiskView').then((module) => {
    resolve(module)
  })
}

const AboutUsView = (resolve) => {
  import('@/views/AboutUsView').then((module) => {
    resolve(module)
  })
}

const ProtoitemView = (resolve) => {
  import('@/views/ProtoitemView').then((module) => {
    resolve(module)
  })
}

const commonHeader = (resolve) => {
  import('@/components/commonHeader').then((module) => {
    resolve(module)
  })
}

import MapView from '@/views/MapView'

let router = new Router({
  mode:'history',
  base:'/mobile',
  routes: [
    {
      path: '/',
      redirect: '/index',
    },
    {
      path:'/index',
      component:Layout,
      children:[
        {
          path: '/',
          meta:{
            keepAlive:true
          },
          component: IndexView
        }
      ]
    },
    {
      path:'/guide',
      component:Layout,
      children:[
        {
          path: '/',
          meta:{
            keepAlive:true
          },
          component: GuideView
        }
      ]
    },
    {
      path:'/productDetail',
      component: ProductDetailView
    },
    {
      path:'/search',
      component:SearchView
    },
    {
      path:'/mine',
      component: Layout,
      meta:{
        requireAuth:true
      },
      children:[
        {
          path: '/',
          meta:{
            keepAlive:true
          },
          component: MineView
        }
      ]
    },
    {
      path:'/login',
      component:LoginView,
    },
    {
      path:'/sponsor',
      meta:{
        keepAlive:true
      },
      component:SponsorView,
    },
    {
      path:'/newuser',
      meta:{
        keepAlive:true
      },
      component:NewUserView
    },
    {
      path:'/datareport',
      component:DataReportView
    },
    {
      path:'/userset',
      meta:{
        keepAlive:true
      },
      component:UserSetView
    },
    {
      path:'/userauth',
      meta:{
        keepAlive:true
      },
      component:UserAuthView
    },
    {
      path:'/succase',
      meta:{
        keepAlive:false
      },
      component:SucCaseView
    },
    {
      path:'/message',
      meta:{
        keepAlive:false
      },
      component:MessageView
    },
    {
      path: '/messagedetail',
      component:MessageDetailView
    },
    {
      path: '/myorder',
      component:MyOrderView
    },
    {
      path: '/myaddress',
      component:MyAddressView
    },
    {
      path: '/myproject',
      component:MyProjectView
    },
    {
      path: '/mycaledar',
      component:MyCalendarView
    },
    {
      path: '/riskwarn',
      component: RiskView
    },
    {
      path: '/linkus',
      component:LinkUsView
    },
    {
      path: '/aboutus',
      component:AboutUsView
    },
    {
      path: '/protoitem',
      component:ProtoitemView,
      meta:{
        requireAuth:true
      },
    },
    {
      path: '/map',
      component:MapView
    }
  ],
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})

router.beforeEach((to, from, next) => {
  //store.commit('updateLoadingStatus', {isLoading: true})  //页面跳转菊花图
    if (to.matched.some(r=>r.meta.requireAuth)) {  // 判断该路由是否需要登录权限
        if (store.state.user.loginStatus){
            next();
        }
        else {
            next({
                path: '/login',
                query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
            })
        }
    }
    else {
        next();
    }
    if (to.matched.length === 0) { //如果未匹配到路由
      from.name ? next({ name: from.name }) : next('/'); //如果上级也未匹配到路由则跳转登录页面，如果上级能匹配到则转上级路由
    } else {
      next(); //如果匹配到正确跳转
    }
})

router.afterEach(function (to) {
  //store.commit('updateLoadingStatus', {isLoading: false})
  var _hmt = _hmt || [];
   (function() {
    //每次执行前，先移除上次插入的代码
    document.getElementById('baidu_tj') && document.getElementById('baidu_tj').remove();
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?c91a5b7fa3f274ab98c443a2feb32491";
    hm.id = "baidu_tj"
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
   })();
})

export default router;